package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.bean.WorkTimeBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.WorkTime;
import com.sp.scmdb.service.WorkTimeService;

@Controller("worktimeAction")
@Scope("prototype")
public class WorkTimeAction extends BaseAction<WorkTimeBean> {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(WorkTimeAction.class);

	private WorkTimeService workTimeService;

	@Autowired
	public void setWorkTimeService(WorkTimeService workTimeService) {
		this.workTimeService = workTimeService;
	}

	public String findAll() {
		try {
			List<WorkTimeBean> depList = workTimeService.queryAll();
			entity.setAllList(depList);
			entity.setList(depList.subList(entity.getCurrentRow(), entity.getLastRow()));

			this.button("Super.Popup(5,'添加工时','/SCMDB/worktimeAction!addInitWT.action','',400,200)",
					"Super.editData('/SCMDB/worktimeAction!findWT.action?id=',400,200)",
					"Super.delData('/SCMDB/worktimeAction!delWT.action?id=')");
		} catch (RuntimeException e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "error";
		}
		return SUCCESS;
	}

	public String addInitWT() {
		return "addSucc";
	}

	/**
	 * 添加工时
	 */
	public String addWT() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = workTimeService.newWT(entity, user);
			if (log.isInfoEnabled()) {
				log.info("add suncce");
			}
			messageSession(result, "工时");
		} catch (RuntimeException e) {
			log.error("addWT fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("addWT fail!reason:" + e.getMessage());
			return "fail";
		}
		return "commitSucc";
	}

	/**
	 * 跳转至工时修改页面
	 */
	public String findWT() {
		try {
			String depId = request.getParameter("id");
			WorkTime workTime = workTimeService.findID(depId);
			if (null != workTime) {
				entity.setId(String.valueOf(workTime.getId()));
				entity.setName(workTime.getName());
				entity.setType(workTime.getType());
				entity.setHourLen(workTime.getHourLen());

			} else {
				// 如果工时不存在，返回到查询页面
				return "commitSucc";
			}
		} catch (RuntimeException e) {
			log.error("findWT fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("findWT fail!reason:" + e.getMessage());
			return "fail";
		}
		return "editSucc";
	}

	/**
	 * 更新工时
	 */
	public String editWT() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = workTimeService.editWT(entity, user);
			messageSession(result, "工时");
		} catch (RuntimeException e) {
			log.error("editWT fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("editWT fail!reason:" + e.getMessage());
			return "fail";
		}
		return "commitSucc";
	}

	/**
	 * 删除工时
	 */
	public String delWT() {
		try {
			UserBean user = (UserBean) session.get("user");
			String depId = request.getParameter("id");
			if (StringUtils.isNotEmpty(depId)) {
				String result = workTimeService.delWT(depId.split(","), user);
				if ("10".equals(result)) {
					messageSession("0", "该工时有所属人员，不能删除！");
					return "commitSucc";
				}
				messageSession(result, "工时");
			}
		} catch (RuntimeException e) {
			log.error("delWT fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("delWT fail!reason:" + e.getMessage());
			return "fail";
		}
		return "commitSucc";
	}
}
